package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.core.zn_internal;
    import components.utils.LabelItemUtil;
    
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [Event(name="select", type="flash.events.Event")]
    public class BaseItemRenderer extends UIComponent
    {
        protected var _data:Object;

        protected var _selected:Boolean = false;

		public var labelField:String="";
		
        /**
         *能否被选中
         */
        protected var _selectedPossible:Boolean = true;

        /**
         *设置数据时的回调
         * function(this,data);
         */
        public var dataFunction:Function;

        public function BaseItemRenderer()
        {
            super();

            left = right = 0;
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
        }

        protected override function measureSizes():Boolean
        {
            if (super.measureSizes())
            {
                if (labelTF)
                {
                    labelTF.width = width - labelTF.x - 2;
                }
                return true;
            }

            return false;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        /***************************************
         *
         * 事件
         *
         * **************************************/

        protected override function mouseClickHandler(event:MouseEvent):void
        {
            super.mouseClickHandler(event);
            selected = true;
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/


        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);
            labelTF.mouseChildren = labelTF.mouseEnabled = false;
        }

        public function get data():Object
        {
            return _data;
        }

        public function set data(value:Object):void
        {
            _data = value;
            if (dataFunction != null)
                dataFunction.call(this, this, value);
            else
            {
                if (labelField!="" && labelField!=null && data && data.hasOwnProperty(labelField))
                    labelStr = data[labelField];
                else
					labelStr= LabelItemUtil.getLabel(data);
            }


            invalidate();
        }

        public function get selected():Boolean
        {
            return _selected;
        }

        public function set selected(value:Boolean):void
        {
            zn_internal::selected = value;

            dispatchEvent(new Event(Event.SELECT, true));
        }

        zn_internal function set selected(value:Boolean):void
        {
            if (selectedPossible)
            {
                if (selected == value)
                    return;
                _selected = value;
            }

            rollOutHandler(null);
        }

        public function get selectedPossible():Boolean
        {
            return _selectedPossible;
        }

        public function set selectedPossible(value:Boolean):void
        {
            _selectedPossible = value;
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;
            if (labelTF)
                labelTF.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();
            _data = null;
        }

        public override function validateNow():void
        {
            super.validateNow();
            labelTF.validateNow();
        }

        public function set labelStr(value:String):void
        {
            if (labelTF)
                labelTF.text = value;
        }
    }
}